<?php
class PostsController extends BlogAppController {

	var $name = 'Posts';

	public function beforeFilter(){
		parent::beforeFilter();
	}
	
	function index() {
		$this->Post->recursive = 1;
		$conditions = array();
		if($this->Session->check('Auth.User.id') && $this->Session->read('Auth.User.group') == 'admin' || $this->Session->check('Auth.User.id') && $this->Session->read('Auth.User.group') == 'manag'){
			$this->redirect(array('action' => 'admin_index'));
		}else{
			$conditions['Post.status'] = 1;
		}
		$this->paginate = array('limit' => 40,'order'=>'Post.added','conditions'=>$conditions);
		$posts = $this->paginate('Post');
		$this->set('posts', $posts);
		
	}
	function admin_index() {
		$this->Post->recursive = 1;
		$this->paginate = array('limit' => 40,'order'=>'Post.added');
		$posts = $this->paginate('Post');
		$this->set('posts', $posts);
		
	}
	
	
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('post', $this->Post->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->data['Post']['added'] = date('Y-m-d H:i:s');
			$this->Post->create();
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid post', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			$this->data['Post']['modified'] = date('Y-m-d H:i:s');
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Post->read(null, $id);
		}
	}
	
	function activate(){
		if($this->RequestHandler->isAjax()) {
			Configure::write('debug', 0);
			$this->autoRender = false;
			$this->log(print_r($this->data,true));
			if ($this->Post->save($this->data)) {
				$this->Session->setFlash(__('The post has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The post could not be saved. Please, try again.', true));
			}
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for post', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Post->deleteAll(array('Post.id'=>$id),true)) {
			$this->Session->setFlash(__('Post deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Post was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>